//
//  YXNewFeatureViewController.swift
//  微博YX
//
//  Created by apple on 15/11/26.
//  Copyright © 2015年 lyx. All rights reserved.
//

import UIKit



class YXNewFeatureViewController: UICollectionViewController {
//    图片的数量
    private let imgCount = 4
//    唯一标示
    private var ReuseIdentifier = "reuseIdentifier"
//    流水布局
    private let flowlayout = UICollectionViewFlowLayout()
    
    init(){
        // 调用需要流水布局的构造函数,自己来提供流水布局
        super.init(collectionViewLayout:flowlayout)
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

     prepareCollectionView()
        
      

      
    }
    
    
    /// MARK: - 准备ui
    private func prepareCollectionView(){
//      注册
          self.collectionView!.registerClass(YXNewFeatureCell.self, forCellWithReuseIdentifier: ReuseIdentifier)
    
//        设置布局
        flowlayout.itemSize = view.bounds.size
//        滚动方向
        flowlayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
//        设置间距
        flowlayout.minimumInteritemSpacing = 0
        flowlayout.minimumLineSpacing = 0
//        分页
        collectionView?.pagingEnabled = true
//        取消弹簧效果
        collectionView?.bounces = false
    }

    
    // MARK: UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imgCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
   
        
       // dequeueReusableCellWithReuseIdentifier: 从缓存池中加载cell,如果缓存池中没有就使用注册的cell类型来创建一个cell
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ReuseIdentifier, forIndexPath: indexPath) as! YXNewFeatureCell
        cell.index = indexPath.item
        
        return cell
    }
/// MARK: - 动画效果
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
//        获得正在显示的cell
        let visibleIndexPath = collectionView.indexPathsForVisibleItems().last!
        if (visibleIndexPath.item == imgCount-1) {
            let cell = collectionView.cellForItemAtIndexPath(visibleIndexPath) as! YXNewFeatureCell
            cell.startAnimation()
        }
    }
    
}
/// 自定义cell
class YXNewFeatureCell: UICollectionViewCell {
//    按钮点击事件
    func btnClick(){
        print("点击开始按钮")
        AppDelegate.switchViewController(true)
    }
    
    var index: Int = 0 {
        didSet {
            bgImg.image = UIImage(named: "new_feature_\(index + 1)")
            startButton.hidden = true
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   /// 开始动画
    func startAnimation(){
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
    UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
        self.startButton.transform = CGAffineTransformIdentity
        }) { (_) -> Void in
            print("动画完成")
        }
    }
    ///    准备ui
    private func prepareUI() {
//     添加子控件
        contentView.addSubview(bgImg)
        contentView.addSubview(startButton)
        
        bgImg.translatesAutoresizingMaskIntoConstraints = false
        startButton.translatesAutoresizingMaskIntoConstraints = false
//        添加约束
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[biv]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["biv" : bgImg]))
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[biv]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["biv" : bgImg]))
//        开始按钮
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        

    }
    
    /// MARK: -懒加载
    private lazy var bgImg: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
//    开始按钮
    private lazy var startButton: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
//        设置文字
        btn.setTitle("开始体验", forState: UIControlState.Normal)
//        添加点击事件
        btn.addTarget(self, action: "btnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
}
